#!/bin/sh
#
# $Id: AFSConfig,v 1.2 99/05/09 14:49:54 abe Exp $
#
# AFSConfig: configure for AFS

AFSD=/usr/vice/etc/afsd
AH=AFSHeaders
AV=AFSVersion
STD=/usr/afsws/include

# Establish trap and stty handling.

ISIG=":"
trap 'rm -f $AH $AV; $ISIG; exit 1'  1 2 3 15
stty -a 2>&1 | grep isig > /dev/null
if test $? -eq 0
then
  stty -a 2>&1 | egrep -e -isig > /dev/null
  if test $? -eq 0
  then
    ISIG="stty -isig"
    stty isig
  fi
fi

# Decide how to use echo.

ECHO=`echo -n ""`
if test "X$ECHO" = "X-n "
then
  EC="\c"
  EO=""
else
  EC=""
  EO="-n"
fi

# Decide (perhaps for a second time) that AFS is installed.

CELL=""
if test -r /usr/vice/etc/ThisCell
then
  cell=`awk '{print $1}' /usr/vice/etc/ThisCell`
  if test -d /afs/$cell
  then
    CELL=$cell
  else
    CELL=`echo $cell | sed 's/\([^.]*\)\..*/\1/'`
    if test "X$CELL" != "X"
    then
      if test ! -d /afs/$CELL
      then
        CELL=""
      fi
    fi
  fi
fi
if test "X$CELL" = "X"
then
  echo ""
  echo "This system does not appear to have AFS installed."
  exit 1
fi

# See if AFS configuration is wanted.

cat << .CAT_MARK

AFS appears to be installed on this system; cell name "$CELL".

Lsof needs to be configured for AFS by identifying: 1) the directory
that includes the AFS header files needed to compile AFS support into
lsof; and 2) the version of AFS that is installed.
.CAT_MARK

END=0
while test $END = 0
do
  echo ""
  echo $EO "Do you want to configure lsof for AFS (y|n) [y]? $EC"
  read ANS EXCESS
  if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
  then
    exit 1
  fi
  if test "X$ANS" = "Xy" -o "X$ANS" = "XY" -o "X$ANS" = "X"
  then
    END=1
  else
    echo ""
    echo "Please answer y or n."
  fi
done

# See if $AH exists and points to a likely place.

AHOK=""
echo ""
echo "====================================================================="
echo ""
if test -r $AH
then
  AHP=`cat $AH`
  if test -r $AHP
  then
    if test -r $AHP/afs/afs.h
    then
      cat << .CAT_MARK
The location of the AFS header files required by lsof has been
previously identified as "$AHP".

Since <afs/afs.h> can be found there, that's probably correct.
.CAT_MARK

      END=0
      while test $END = 0
      do
	echo ""
	echo $EO "Do you want to use $AHP again (y|n) [y]? $EC"
	read ANS EXCESS
	if test "X$ANS" = "Xy" -o "X$ANS" = "XY" -o "X$ANS" = "X"
	then
	  AHOK="ok"
	  END=1
	else
	  if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
	  then
	    rm -f $AH
	    AHP=""
	    END=1
	  else
	    echo ""
	    echo "Please answer y or n."
	  fi
	fi
      done
    else
      echo "\"$AHP\" has been previously specified as the location of the"
      echo "AFS header files, but it lacks an afs/afs.h header file."
      rm -f $AH
      AHP=""
    fi
  else
    echo "The file ./$AH exists, but has no AFS header file path in it."
    rm -f $AH
    AHP=""
  fi
else
  echo "No previous header location has been specified."
  rm -f $AH
  AHP=""
fi

# See if the header files are in the "standard" place.

if test "X$AHOK" != "Xok"
then
  if test -r $STD
  then
    echo ""
    echo "====================================================================="
    echo ""
    echo "The AFS header files appear to be in the \"standard\" location --"
    echo "i.e.: \"$STD\"."
    END=0
    while test $END = 0
    do
      echo ""
      echo $EO "Do you want to let lsof use them (y|n) [y]? $EC"
      read ANS EXCESS
      if test "X$ANS" = "Xy" -o "X$ANS" = "XY" -o "X$ANS" = "X"
      then
	echo $STD > $AH
	AHOK="ok"
	END=1
      else
	if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
	then
	  END=1
	else
	  echo ""
	  echo "Please answer y or n."
	fi
      fi
    done
  fi
fi

# Ask for the AFS header file location.

if test "X$AHOK" != "Xok"
then
  echo ""
  echo "====================================================================="
  echo ""
  echo "Please specify the full path where lsof can find the AFS header"
  echo "files.  A possible location is: \"/afs/$CELL/<sysname>/include\"."
  cat << .CAT_MARK
The <sysname> component of the path is the AFS system name that
was used to configure and build AFS on this system.  It is usually
constructed from a manufacturer or Unix operating system designation,
followed by a version number -- e.g., hp800_ux90, sun4m_54, vax_ul43,
etc.  You may have to consult your AFS documentation to determine
what <sysname> applies to your configuration.
.CAT_MARK

  END=0
  while test $END = 0
  do
    echo ""
    echo $EO "Do you want to see the contents of /afs/$CELL (y|n) [y]? $EC"
    read ANS EXCESS
    if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
    then
      END=1
    else
      if test "X$ANS" = "Xy" -o "X$ANS" = "XY" -o "X$ANS" = "X"
      then
	echo ""
	ls -C /afs/$CELL
	END=1
      else
	echo ""
	echo "Please answer y or n."
      fi
    fi
  done
  END=0
  while test $END = 0
  do
    echo ""
    echo $EO "AFS header file path: $EC"
    read ANS EXCESS
    fc=`expr "${ANS}X" : '\(.\).*'`
    if test "X$fc" = "X/"
    then
      if test -r $ANS/afs/afs.h
      then
	echo $ANS > $AH
	AHOK="ok"
	END=1
      else
	echo ""
	echo "$ANS/afs/afs.h doesn't exist."
	echo "Please enter a path whose afs subdirectory contains afs.h"
      fi
    else
      echo ""
      echo "Please enter an absolute path name."
    fi
  done
fi
if test "X$AHOK" != "Xok"
then
  echo "AFSConfig: unknown error"
  exit 1
fi

# Determine AFS version.

if test -r $AV
then
  echo ""
  echo "====================================================================="
  echo ""
  AVN=`cat $AV`
  cat << .CAT_MARK
The AFS version was previously specified as: $AVN
.CAT_MARK

  END=0
  while test $END = 0
  do
    echo ""
    echo $EO "Is this the correct version number (y|n) [y]? $EC"
    read ANS EXCESS
    if test "X$ANS" = "Xy" -o "X$ANS" = "XY" -o "X$ANS" = "X"
    then
      exit 0
    fi
    if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
    then
      rm -f $AV
      END=1
    else
      echo "Please answer y or n."
    fi
  done
fi

# See if the version number can be determined.

if test -r $AFSD
then
  ANS=`strings $AFSD | grep "Base configuration afs" | sed 's/^.*ion afs\([^ ]*\) .*/\1/'`
  TV=`echo $ANS | sed 's/^\([0-9]*\)\.\([0-9]*\)\(.*\)/\1 \2 \3/' | awk '{printf "%d.%d%s\n",$1,$2,$3}'`
  if test "X$ANS" = "X$TV"
  then
    echo ""
    echo "====================================================================="
    echo ""
    cat << .CAT_MARK
Examining $AFSD the AFS version number appears to be: $TV
.CAT_MARK

    END=0
    while test $END = 0
    do
      echo ""
      echo $EO "Do you want to use this version number (y/n) [y]? $EC"
      read ANS EXCESS
      if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
      then
	END=1
      else
	if test "X$ANS" = "Xy" -o "X$ANS" = "XY" -o "X$ANS" = "X"
	then
	  echo $TV > $AV
	  exit 0
	else
	  echo ""
	  echo "Please answer y or n."
	fi
      fi
    done
  fi
fi

# Ask for the version number.

echo ""
echo "====================================================================="
END=0
while test $END = 0
do
  echo ""
  echo $EO "Please enter the AFS version number: $EC"
  read ANS EXCESS
  TV=`echo $ANS | sed 's/^\([0-9]*\)\.\([0-9]*\)\(.*\)/\1 \2 \3/' | awk '{printf "%d.%d%s\n",$1,$2,$3}'`
  if test "X$ANS" = "X$TV"
  then
    echo $TV > $AV
    exit 0
  fi
done
